package com.handpay.zztong.hp.accountcenter.activity;

import android.app.Activity;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.handpay.framework.ClientEngine;
import com.handpay.framework.SecureManager;
import com.handpay.framework.utils.CommonUtils;
import com.handpay.zztong.hp.R;
import com.handpay.zztong.hp.accountcenter.model.modifyphoneno.ModifyPhoneNoModel;
import com.handpay.zztong.hp.base.activity.ZZTongActivity;
import com.handpay.zztong.hp.base.callback.BusinessEngineListener;
import com.handpay.zztong.hp.bean.ActionBar;
import com.handpay.zztong.hp.bean.ResponseBean;
import com.handpay.zztong.hp.config.ZZTConstant;
import com.handpay.zztong.hp.login.model.sendphonecode.SendAuthCodeModel;
import com.handpay.zztong.hp.protocol.BaseProtocolKey;
import com.handpay.zztong.hp.ui.VerificationCodeButton;
import com.handpay.zztong.hp.utils.AccountUtils;

import java.util.Hashtable;

/**
 * 修改手机号码
 */
public class NewPhoneNoConfirmActivity extends ZZTongActivity {
    private EditText original_mobile_number;
    private EditText messagecode;
    private VerificationCodeButton getmassagecode;
    private String oldPhone = null;
//    private AuthCodeBill authCodeBill;

    @Override
    public ActionBar getActivityActionBar() {
        return new ActionBar(this.getString(R.string.Update_phone), true);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        this.setContentView(R.layout.activity_new_phone_no_confirm);
        super.onCreate(savedInstanceState);
        original_mobile_number = (EditText) findViewById(R.id.original_mobile_number);
        messagecode = (EditText) findViewById(R.id.messagecode);
        getmassagecode = (VerificationCodeButton) findViewById(R.id.getmassagecode);
        Button confirm = (Button) findViewById(R.id.btn_confirm);
        getmassagecode.setOnClickListener(this);
        confirm.setOnClickListener(this);
        Initialization();
//        authCodeBill = new AuthCodeBill(this);
    }

    protected boolean isShownTabs() {
        return false;
    }

    private void Initialization() {
        this.oldPhone = getIntent().getStringExtra("oldPhone");
    }

    private void doSendPhoneCode() {
        if (!CommonUtils.isMobileNum(original_mobile_number.getText().toString().trim())) {
            this.showAlertDialog(this, R.string.tip, R.string.input_right_phone_tip);
            return;
        }
//        authCodeBill.sendAuthCode(ZZTConstant.AUTHCODE_ACTION_NEWPHONE, original_mobile_number.getText().toString().trim());
        new SendAuthCodeModel().doSendAuthCodePost(this, ZZTConstant.AUTHCODE_ACTION_NEWPHONE, original_mobile_number.getText().toString().trim(), new BusinessEngineListener<ResponseBean>() {
            @Override
            public void callbackBusinessResponse(ResponseBean bean) {
                if (bean.getResponseCode().equals("0")) {
                    Toast.makeText(NewPhoneNoConfirmActivity.this, "发送验证码成功！", Toast.LENGTH_SHORT).show();
                } else {
                    showAlertDialog(NewPhoneNoConfirmActivity.this, getString(R.string.prompt), bean.getErrMessage());
                }
                setResult(Activity.RESULT_OK);
            }
        });
        getmassagecode.startLocker(-1);
    }

    private void DOzztModifyPhoneNum() {
        Hashtable<String, String> params = new Hashtable<>();
        params.put("phone", SecureManager.getInstance().des(oldPhone, 1, null));
        params.put(BaseProtocolKey.ACCOUNT, SecureManager.getInstance().des(AccountUtils.getAccountName(this), 1, null));
        params.put("action", ZZTConstant.AUTHCODE_ACTION_NEWPHONE);
        params.put("newPhone", SecureManager.getInstance().des(original_mobile_number.getText().toString().trim(), 1, null));
        params.put("channel", ClientEngine.CHANNEL);
        params.put("verifyCode", messagecode.getText().toString().trim());
//        doJsonPost(this, DO_zztModifyPhoneNum, ResponseBean.class, params);
        new ModifyPhoneNoModel().doModifyPhoneNoPost(this, params, new BusinessEngineListener<ResponseBean>() {
            @Override
            public void callbackBusinessResponse(ResponseBean bean) {
                AccountUtils.setAccountPhone(NewPhoneNoConfirmActivity.this, original_mobile_number.getText().toString().trim());
                showAlertDialog(NewPhoneNoConfirmActivity.this, getString(R.string.tip), getString(R.string.update_phone_tip_Success), false, new OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        Intent intent = new Intent(NewPhoneNoConfirmActivity.this, BaseInfoActivity.class);
                        intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                        NewPhoneNoConfirmActivity.this.startActivity(intent);
                        dialog.dismiss();
                    }
                });
            }
        });
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
    }

    private boolean IsControlValues() {
        if (!CommonUtils.isMobileNum(original_mobile_number.getText().toString())) {
//			this.showAlertDialog(this, R.string.tip,R.string.input_right_phone_tip);
            Toast.makeText(this, R.string.input_right_phone_tip, Toast.LENGTH_SHORT).show();
            return false;
        } else if (messagecode.getText().toString().trim().length() == 0) {
            Toast.makeText(this, R.string.input_right_message_code_tip, Toast.LENGTH_SHORT).show();
            return false;
        } else if (oldPhone == null) {
            Toast.makeText(this, R.string.input_right_old_phone_tip, Toast.LENGTH_SHORT).show();
            return false;
        } else if (oldPhone.equals(original_mobile_number.getText().toString().trim())) {
            Toast.makeText(this, R.string.original_mobile_or_new_phone, Toast.LENGTH_SHORT).show();
            return false;
        }

        return true;
    }

    @Override
    public void onClick(View v) {
        int i = v.getId();
        if (i == R.id.getmassagecode) {//
            doSendPhoneCode();

        } else if (i == R.id.btn_confirm) {//
            if (IsControlValues()) {
                DOzztModifyPhoneNum();
            }

        }
    }

}
